<template>
    <!-- 组件的模板 -->
    <div class="weui-search-bar" v-if="items.length">
        <form class="weui-search-bar__form" @click="gotoSearchList">
            <div class="weui-search-bar__box">
                <i class="weui-icon-search"></i>
                <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required="">
                <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
            </div>
            <label class="weui-search-bar__label" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);">
                <i class="weui-icon-search"></i>
                <span>搜索</span>
            </label>
        </form>
    </div>
    <div class="page" v-if="items.length">   
        <ul class="user-list">
            <li class="com-border-bottom" v-for='item in items' @click="gotoCv(item.user_id,item.doumi_user_id)">
                <a class="touch" href="javascript:;">
                    <img class="avatar" v-if="item.logo" :src="item.logo" alt="">
                    <img class="avatar" v-else src="http://sta.doumistatic.com/src/image/saas/avatar.png" alt=""> 
                    <h2>{{item.real_name}}
                        <span class="bg_sprite gender female" v-if="item.gender == '女'"></span>
                        <span class="bg_sprite gender male" v-else></span>
                    </h2>
                    <p>
                        <span v-if="item.age" class="tag">{{item.age}}岁</span>
                        <span class="tag">{{item.city}} {{item.district}}</span>
                    </p>
                </a>
            </li>
        </ul>  
        <div class="page__bd" v-show="showMore">
          <div class="weui-loadmore">
              <i class="weui-loading"></i>
              <span class="weui-loadmore__tips">正在加载</span>
          </div>
        </div>
    </div>

    <div class="page empty-page" v-if="empty">
        <div class="mod-empty new_mod-empty2">
            <div class="icon_3"></div>
            <p class="tip">空空如也，快去创建你的企业吧</p>            
            <a href="javascript:;" class="weui-btn weui-btn_primary createLibrary-btn" @click="gotoTeamCreate">创建企业</a>
        </div>
    </div>

    <div class="page empty-page" v-if="empty2">
        <div class="mod-empty">
            <div class="icon icon_7"></div>
            <p class="tip">还没有人才简历哦</p>
        </div>
    </div> 
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import widget from 'gapi/widget.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    let teamid = ''
    let offset = 1 // 返回记录行的偏移量(从第几条数据开始取
    let rows = 20 // 返回记录行的最大条数（取的条数）
    let canLoadMore = 1
    /**
     *  组件实例对象
     */
    export default { 
        components: {

        },
        data: function(){
            return{
                empty:false,
                empty2:false,
                showMore:false,
                user_id:'',
                items:[],
                message:'暂无人才简历',
                searchEmpty:''
            }
        },
        vuex: {
            getters: {

            },
            actions: { 

            }
        },
        computed: {
            params (){
                let str = 'teamId='+teamid+'&userId='+this.item.user_id;
                return encodeURIComponent(str)
            }
        },
        methods: {
            /** 
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                commonPage.canPullWebView(true)
                teamid = util.getQueryString('teamId')
                if(teamid == -1 || teamid == 0){
                    this.empty = true
                    this.empty2 = false
                    commonPage.canPullWebView(false)
                    return
                }else{
                    this.initAjax()
                }
                
                
                window.loadData = () => {
                    commonPage.dataDownloadFinished()
                    offset = 1
                    this.initAjax()
                }
            },
            // 初始化人才库列表
            initAjaxMore (mark){
                setTimeout(()=>{
                    util.ajax({
                        url:'/talent/list',
                        type:'GET',
                        data:{
                            team_id:teamid,  
                            project_id:0,
                            offset:offset, 
                            rows:rows   
                        },
                        timeout:10000,
                        success:(obj) => {
                            console.log(obj)
                            commonPage.dataDownloadFinished()
                           this.showMore = false
                           if(obj&&obj.errno == 0){
                                if(obj.data.length){
                                    setTimeout( ()=>{
                                    obj.data.forEach((item)=>{
                                        this.items.push(item) 
                                    })
                                    },100)
                                    offset+=rows
                                }else{
                                    canLoadMore = 0
                                }
                            }
                            // else{
                                // widget.toast(obj.errmsg)
                            // }
                        },
                        error: (xhr, status) => {
                            commonPage.dataDownloadFinished()
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            commonPage.dataDownloadFinished()
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                },1000)
            },
            initAjax (){
                let _data = {
                    team_id:teamid,  
                    project_id:0,
                    offset:offset, 
                    rows:rows  
                }
                console.log(_data)
                if(teamid == -1){
                    this.empty = true
                    this.empty2 = false
                    commonPage.canPullWebView(false)
                    return
                }
                util.ajax({
                    url:'/talent/list',
                    type:'GET',
                    data:_data,
                    timeout:10000,
                    success:(obj) => {
                        commonPage.dataDownloadFinished()
                       this.showMore = false
                       if(obj&&obj.errno == 0){
                            if(obj.data.length){
                                this.items = obj.data
                                offset+=rows
                                this.empty = false
                                this.empty2 = false
                            }else{
                                this.empty = false
                                this.empty2 = true
                                canLoadMore = 0
                            }
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.dataDownloadFinished()
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.dataDownloadFinished()
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            /*跳转到简历*/
            gotoCv (userId,doumiUserId){
                let encodeStr = encodeURIComponent(`doumiUserId=${doumiUserId}&userId=${userId}`)
                location.href = `dmsaas://TalentDetail?urdData=${encodeStr}`
            },
            gotoSearchList (){
                let encodeStr = encodeURIComponent(`teamId=${teamid}&searchEmpty=${this.searchEmpty}`)
                location.href = `dmsaas://TalentPoolSearch?urdData=${encodeStr}`
            },
            /*前往创建团队*/
            gotoTeamCreate (){
                let url = `backWhereParam=${333}&user_id=${this.user_id}`
                location.href = "dmsaas://CreateTeam?urdData="+encodeURIComponent(url)
            }
        },
        created(){
            this.init();
            commonPage.registerHitPageBottomListener(() => {
                if(canLoadMore==0){
                    return
                }
                this.showMore = true
                this.initAjaxMore()
            }, 0)

            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('personlibraryWebViewId',{'webViewID':data})
            })
        },
        ready(){
            //初始化的时候请求行业列表数据存储在缓存里
            if(this.empty == true){
                commonPage.loadPageStatus('loading')
                commonPage.canPullWebView(false)
                util.ajax({
                    url:'/common/industry/get',
                    type:'GET',
                    data:{
                        team_id:0,
                        project_id:0,
                        category_id:''
                    },
                    timeout:10000,
                    success:(obj) => {
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj.errno == 0){
                            let objs = obj.data
                            cache.setCache('str', objs, 86400*7)
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => { 
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
            commonPage.loadPageStatus('loading')
            util.ajax({
                url:'/user/info',
                type:'GET',
                data:{
                    team_id:0,
                    project_id:0
                },
                timeout:10000,
                success:(obj) => {
                    // console.log(obj)
                    commonPage.loadPageStatus('loadSuccess')
                    if(obj&&obj.errno == 0){
                        this.user_id = obj.data.user_id
                        if(obj.data.team_id){
                            this.searchEmpty = 1
                        }else{
                            this.searchEmpty = 0
                        }
                    }else{
                        widget.toast(obj.errmsg)
                    }
                },
                error: (xhr, status) => {
                    commonPage.loadPageStatus('loadSuccess')
                    widget.toast('服务器异常')
                },
                noNetwork: () => {
                    commonPage.loadPageStatus('loadSuccess')
                    widget.toast('网络连接失败，请检查网络')
                }
            })
            
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/talent-pool.css"></style>
<style>
    .female{
        display: inline-block;
        width: 12px;
        height: 12px;
        margin-left: 6px;
        background-position: -17px -109px;
    }
    .male{
        display: inline-block;
        width: 12px;
        height: 12px;
        margin-left: 6px;
        background-position: 0px -109px;
    }
    .page .mod-empty .createLibrary-btn{
        width: 269px;
        background: #FFAA00;
        border-radius: 4px;
        font-size: 16px;
    }
</style>



